[hibernate - jpa ] good practices and bad practices

Posted by blow on Stack Overflow See other posts from Stack Overflow or by blow
Published on 2010-06-17T21:06:11Z Indexed on 2010/06/17 21:13 UTC
Read the original article Hit count: 765

Filed under:
|
|
|

Hi all, i have some questions about interaction with hibernate.

  1. openSession or getCurrentSession (without jta, thread insted)?
  2. How mix session operations with swing gui? Is good have something like this in a javabean class?

    public void actionPerformed(ActionEvent event) {
        // session code
    }
    
  3. Can i add methods to my entities that contains hql queries or is a bad practice? For example:

     // This method is in an entity MyOtherEntity.java class
     public int getDuration(){
        Session session=HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int sum=(Integer)session.createQuery("select sum(e.duration) as duration from MyEntity as e where e.myOtherEntity.id=:id group by e.name").
            .setLong("id", getId());
            .uniqueResult();
        return sum;
     }
    

In alternative how can i do this in a better and elegant way?

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate